<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/component/commonTagLib.jsp"%>
<!DOCTYPE html>
<html>
<head>
<title><spring:message code="message.lable.DICyaoLiFenLeiChaKan"/></title>
<%@ include file="/WEB-INF/component/commonCSS.jsp" %>  
<!--[if lt IE 9]>
    <script src="${ctx}/lib/js/html5shiv.min.js"></script>
    <script src="${ctx}/lib/js/respond.min.js"></script>
<![endif]-->
</head>
<body class="skin-blue-light sidebar-mini fixed skin-blue-light-frame">
    <section class="content-header">
        <h1><spring:message code="message.lable.DICyaoLiFenLeiChaKan"/></h1>
        <ol class="breadcrumb">
            <li><a href="${ctx}/home.jsp"><i class="fa fa-home"></i><spring:message code="message.lable.first"/></a></li>
             <li class="active"><a href="#"><spring:message code="message.lable.DicManage"/></a></li>
             <li class="active"><a href="#"><spring:message code="message.lable.pharDic"/></a></li>
        </ol>
    </section>
    <section class="content">
        <div class=" box box-success">
            <div class="box-body">
            <form action="${ctx}/dicPhar/exportExcel.html" id="exportExcel" method="post">
            <input id="fileName" name="fileName" type="hidden" value="<spring:message code="message.lable.pharxls"/>" />
                    <div class="input">
           			<div class="textalign1"><spring:message code="message.listForm.yaoLiFenLeiMingCheng"/>：</div>
                    <div class="boxinput">
                        <input type="text" class="textinput" id="searchPharName" name="pharName" placeholder="<spring:message code="message.listForm.yaoLiFenLeiMingCheng"/>" maxlength="50" />
                    </div>
                    <div class="textalign1"><spring:message code="message.listForm.yaoLiFenLeiBianMa"/>：</div>
                    <div class="boxinput">
                        <input type="text" class="textinput" id="searchPharCode" name="pharCode" placeholder="<spring:message code="message.listForm.yaoLiFenLeiBianMa"/>" maxlength="10" />
                    </div>
                    
                    <div class="textalign1"><spring:message code="message.lable.status"/>：</div>
                    <div class="boxinput">
                       <select id="status" name="status" class="textinput">
                           	<option value="" ><spring:message code="message.select.option"/></option>
                            <option value="1" ><spring:message code="message.button.start"/></option>
                            <option value="0" ><spring:message code="message.button.stop"/></option>
                           </select>
                     </div>
                     <div class="textalign" >
                    <button type="button" class="btn btn-success btn-sm" onclick="search();"><spring:message code="message.button.seachSpacing"/></button>
                </div>
                 </div>
                 </form>
            </div>
        </div>
        <table class="jqgrid" id="gridlist"></table>
        <div class=" box box-warning bottongroup">
             <ms:btn url="/dicPhar/add.html"><label id="addNew" class="btn btn-default btn-sm"><spring:message code="message.button.addSpacing"/></label></ms:btn>
             <ms:btn url="/dicPhar/enable.html"><label id="allStart" class="btn btn-default btn-sm" onclick="changeStatus(1);"><spring:message code="message.button.startSpacing"/></label></ms:btn>
             <ms:btn url="/dicPhar/disable.html"><label id="allDelete" class="btn btn-default btn-sm" onclick="changeStatus(0);"><spring:message code="message.button.stopSpacing"/></label></ms:btn>
			<label onclick="exportExcel();" class="btn btn-default btn-sm"><spring:message code="message.lable.daoChu"/></label>        
        </div>
        <div class="modal fade" id="dialog" tabindex="-1" >
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header bg-primary">
                        <button type="button" class="close" data-dismiss="modal" ><span >&times;</span></button>
                        <h5 class="modal-title"><spring:message code="message.listForm.YaoLiFenLei"/><span id="editTitle"><spring:message code="message.button.add"/></span></h5>
                    </div>
                    <div class="modal-body">
                        <div class="form-group form-group-xs" style="height:50px;">
	                        <div style="width:100%;float:left;">
	                        	<label for="recipient-name" class=" control-label"><spring:message code="message.listForm.fuJiYaoLiFenLei"/>：</label>
	                        </div>
                        	
                            <div style="width:33%;float:left;padding-right:9px;">
                            <select id="rootType" class="form-control">
                            <option value=''><spring:message code="message.select.option"/></option>
                            </select>
                            </div>
                            <div style="width:34%;float:left;padding-right:11px;padding-left:11px;">
                            <select id="dicPhar1" class="form-control">
                            <option value=''><spring:message code="message.select.option"/></option>
                            </select>
                            </div>
                            <div style="width:33%;float:left;padding-left:9px;">
                            <select id="dicPhar2" class="form-control">
                            <option value=''><spring:message code="message.select.option"/></option>
                            </select>
                            </div>
                        </div>
                        <div class="form-group form-group-xs" style="width:50%;float:left;padding-right:10px;">
                            <label for="pharCode" class=" control-label"><spring:message code="message.listForm.yaoLiFenLeiBianMa"/>：</label>
                            <input type="text" class="form-control" id="pharCode" placeholder="<spring:message code="message.listForm.yaoLiFenLeiBianMa"/>" maxlength="10">
                        </div>
                        <div class="form-group form-group-xs" style="width:50%;float:left;padding-left:10px;">
                            <label for="pharName" class=" control-label"><spring:message code="message.listForm.yaoLiFenLeiMingCheng"/>：</label>
                            <input type="text" class="form-control" id="pharName" placeholder="<spring:message code="message.listForm.yaoLiFenLeiMingCheng"/>" maxlength="50">
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-primary btn-sm" onclick="addSave();"><spring:message code="message.button.saveSpacing"/></button>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- 修改 -->
        <div class="modal fade" id="editDialog" tabindex="-1" >
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header bg-primary">
                        <button type="button" class="close" data-dismiss="modal"><span >&times;</span></button>
                        <h5 class="modal-title"><spring:message code="message.listForm.YaoLiFenLei"/><span id="editTitle"><spring:message code="message.jqGrid.cz.bianJi"/></span></h5>
                    </div>
                    <div class="modal-body">
                        <div class="form-group form-group-xs">
                            <label for="pharCode" class=" control-label"><spring:message code="message.listForm.yaoLiFenLeiBianMa"/>：</label>
                            <input type="text" class="form-control" id="editPharCode"  placeholder="<spring:message code="message.listForm.yaoLiFenLeiBianMa"/>" maxlength="10">
                        </div>
                        <div class="form-group form-group-xs">
                            <label for="pharName" class=" control-label"><spring:message code="message.listForm.yaoLiFenLeiMingCheng"/>：</label>
                            <input type="text" class="form-control" id="editPharName" placeholder="<spring:message code="message.listForm.yaoLiFenLeiMingCheng"/>" maxlength="50">
                        </div>
                        <input type="hidden" id="editId"/>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-primary btn-sm" onclick="editSave();"><spring:message code="message.button.saveSpacing"/></button>
                    </div>
                </div>
            </div>
        </div>
    </section>
    <%@ include file="/WEB-INF/component/commonJS.jsp" %>
    <script type="text/javascript">
                var colorList = [];//用来保存grid每一行原来的颜色
        
            function addSave() {
                var rootType = $.trim($("#rootType").val());
                var dicPhar1 = $.trim($("#dicPhar1").val());
                var dicPhar2 = $.trim($("#dicPhar2").val());
                var pharCode = $.trim($("#pharCode").val());
                if (pharCode == null || pharCode == "") {
                    $.alert("<spring:message code="message.yanZheng.qingShuRu"/><spring:message code="message.listForm.yaoLiFenLeiBianMa"/><spring:message code="message.yanZheng.ganTanHao"/>",  "warn");
                    return;
                } else if (!/^[a-zA-Z0-9_]+$/.test(pharCode)) {
                    $.alert("<spring:message code="message.listForm.yaoLiFenLeiBianMa"/><spring:message code="message.yanZheng.zhiNengYouZiMuShuZiZuCheng"/>", "warn");
                    return;
                }
                var pharName = $.trim($("#pharName").val());
                if (pharName == null || pharName == "") {
                    $.alert("<spring:message code="message.yanZheng.qingShuRu"/><spring:message code="message.listForm.yaoLiFenLeiMingCheng"/><spring:message code="message.yanZheng.ganTanHao"/>", "warn");
                    return;
                } 
                var fatherId = "";
                if(!rootType){
                    fatherId = "0";
                }else if(!dicPhar1){
                    fatherId = rootType;
                }else if(!dicPhar2){
                    fatherId = dicPhar1;
                }else {
                    fatherId = dicPhar2;
                }
                $.HN.message.confirm("<spring:message code="message.HN.alert.OkSaveYESorNO"/>", '', '').on(function (e) {
                    if (e) {
                        $.post("${pageContext.request.contextPath}/dicPhar/add.html", 
                                {
                                    "fatherId":fatherId,
                                    "pharCode":pharCode,
                                    "pharName":pharName,
                                }, 
                            function(result) {
                                if (result.success) {
                                    search();
                                    $('#dialog').modal('hide');
                                    $.alert("<spring:message code="message.HN.alert.success"/>", "success");
                                } else {
                                    $.alert(result.msg || "<spring:message code="message.HN.alert.fail"/>", "error");
                                }
                        }, "json");
                    }
                });
            }
    
            
            function toEdit(id,pharCode,pharName) {
                $("#editPharCode").val(pharCode);
                $("#editPharName").val(pharName);
                $("#editId").val(id);
                $('#editDialog').modal({
                    keyboard: true
                });
            }
            
            function editSave(){
                var pharCode = $.trim($("#editPharCode").val());
                if (pharCode == null || pharCode == "") {
                    $.alert("<spring:message code="message.yanZheng.qingShuRu"/><spring:message code="message.listForm.yaoLiFenLeiBianMa"/><spring:message code="message.yanZheng.ganTanHao"/>",  "warn");
                    return;
                } else if (!/^[a-zA-Z0-9_]+$/.test(pharCode)) {
                    $.alert("<spring:message code="message.listForm.yaoLiFenLeiBianMa"/><spring:message code="message.yanZheng.zhiNengYouZiMuShuZiZuCheng"/>", "warn");
                    return;
                } 
                var pharName = $.trim($("#editPharName").val());
                if (pharName == null || pharName == "") {
                    $.alert("<spring:message code="message.yanZheng.qingShuRu"/><spring:message code="message.listForm.yaoLiFenLeiMingCheng"/><spring:message code="message.yanZheng.ganTanHao"/>", "warn");
                    return;
                } 
                $.HN.message.confirm("<spring:message code="message.HN.alert.OkSaveYESorNO"/>", '', '').on(function (e) {
                    if (e) {
                        $.post("${pageContext.request.contextPath}/dicPhar/edit.html", 
                                {"pharCode":pharCode,"pharName":pharName,"pharId":$("#editId").val()}, 
                                function(result) {
                                    if (result.success) {
                                        search();
                                        $('#editDialog').modal('hide');
                                        $.alert("<spring:message code="message.HN.alert.success"/>", "success");
                                    } else {
                                        $.alert(result.msg || "<spring:message code="message.HN.alert.fail"/>", "error");
                                    }
                                }, "json"
                            );
                    }
                });
                
            }
            
            function search() {
                var pharCode = $.trim( $("#searchPharCode").val());
                var pharName = $.trim( $("#searchPharName").val());
                var status = $.trim( $("#status").val());
                $("#gridlist").jqGrid('setGridParam',{  
                    datatype:'json',
                    postData:{"pharCode":pharCode, "pharName":pharName,n_level:-1,"status":status}
                }).trigger("reloadGrid"); //重新载入  
            }
            
            //清空新增值
            function cleanForm() {
                $("#rootType").val("");
                $("#dicPhar1").val("");
                $("#dicPhar2").val("");
                $("#dicPhar1 option:not(:first)").remove();
                $("#dicPhar2 option:not(:first)").remove();
                $("#pharCode").val("");
                $("#pharName").val("");
            }
    
            $(function () {
            	$("#searchPharName").focus();//加载页面光标在第一个查询框上
                
                
                $('#addNew').click(function () {
                    select();
                    cleanForm();
                    $('#dialog').modal({
                        keyboard: true
                    });
                });
                $("#gridlist").jqGrid({  
                     treeGrid: true,  
                        treeGridModel: 'adjacency', //treeGrid模式，跟json元數據有關 ,adjacency/nested  
                        ExpandColumn : 'pharCode',  
                        url: "${pageContext.request.contextPath}/dicPhar/getDrugPharList.html",
                        datatype: 'json',  
                        mtype: "POST",  
                        postData : {
                               n_level:-1  
                        },
                        ExpandColClick: true,  
                        autowidth: true,
                        height: 270,
                        colNames: [ '',
                                    '<spring:message code="message.jqGrid.cz"/>',
                                    '','',
                                    '<spring:message code="message.listForm.yaoLiFenLeiBianMa"/>',
                                    '<spring:message code="message.listForm.yaoLiFenLeiMingCheng"/>',
                                    '<spring:message code="message.lable.status"/>'
                                    ],
                        colModel: [
                            { name: 'check', width: 1, align:"center", sortable: false,
                                formatter : function(val, opts, rowdata) {
                                    return "<input type='checkbox' id='w"+rowdata[3]+"'  name='check' value='"+rowdata[3]+"' onclick='changeColor(this);'/><input type='hidden' id='h"+rowdata[3]+"' value='"+rowdata[2]+"'/><input type='hidden' id='s"+rowdata[3]+"' value='"+rowdata[6]+"'/><input type='hidden' id='n"+rowdata[3]+"' value='"+rowdata[5]+"'/>";
                                }
                            },
                            { name: 'operate', width: 6, align:"center", sortable: false,
                                formatter : function(val, opts, rowdata) {
                                    var str =  "<ms:btn url='/dicPhar/edit.html'><a href='javascript:toEdit(\""+rowdata[3]+"\",\""+rowdata[4]+"\",\""+rowdata[5]+"\");' style='background:url(${ctx}/lib/img/icon/icon.png) no-repeat 0px 0px' class='opIcon' title='<spring:message code="message.jqGrid.cz.bianJi"/>'></a></ms:btn>";
                                        str += "<ms:btn url='/dicPhar/log.html'><a href='log.html?id=" + rowdata[3] + "&pharCode="+rowdata[4]+"&pharName="+rowdata[5]+"'  style='background:url(${ctx}/lib/img/icon/icon.png) no-repeat -120px 0px' class='opIcon' title='<spring:message code="message.jqGrid.cz.weiHuJiLu"/>'></a></ms:btn>";
                                    return str;
                            }},
                            { name: 'hierarchy', width: 10,hidden:true, align:"center", sortable: false},
                            { name: 'pharId', width: 10,hidden:true, align:"center", sortable: false},
                            { name: 'pharCode', width: 10, align:"center", sortable: false},
                            { name: 'pharName', width: 10, align:"center", sortable: false},
                            { name: 'status', width: 5, align: 'center', sortable: false,
                            	cellattr:function (rowId, val, rawObject, cm) {
                                    if(val == '停用'){
                                        return 'style="color:red"';
                                    }
                                },

                                formatter : function(val,opts,row){
                                    if(val==1){
                                        return "<spring:message code="message.button.start"/>";
                                    }else if(val==0){
                                        return "<spring:message code="message.button.stop"/>";
                                    }
                                }   
                            }
                            
                        ],    
                        jsonReader: {
                            root: "rows",  
                            total: "total",    
                            repeatitems: true    
                        },  
                    pager: "false",  
                        caption: "<spring:message code="message.list.pharDiclist"/>",    
                        gridComplete: function(){
                            //保存每一行原来的颜色
                            $("input[name='check']").each(function(){
                                var flag = true;
                                for(var i = 0;i<colorList.length;i++){
                                    if($(this).prop("id")==colorList[i].id){
                                        flag = false;
                                        break;
                                    }
                                }
                                if(flag){
                                    colorList.push({"id":"w"+$(this).val(),"color":$("#"+$(this).val()).css("background-color")});
                                }
                                
                            });
                            
                            //避免手动扩展grid自动带入查询参数
                            $("#gridlist").jqGrid('setGridParam',{postData:{"pharCode":"","pharName":"","status":""}});  
                            
                            //单条数据自动选中
                            var rowIds = $("#gridlist").jqGrid('getDataIDs');
                            if(rowIds.length==1){
                                $("#w"+rowIds[0]).click();
                            }
                            
                        }

                }); 
                
                //生成能选择所有的复选框
                $("#jqgh_gridlist_check").remove();
                $("#gridlist_check").append("<input type='checkbox'  id='checkAll'  />");
                
                //选择所有
               $("#checkAll").click(function(){  
                        if($(this).is(':checked')){ 
                            $("input[name='check']").each(function(){
                                $(this).prop("checked",true);//设置选中
                                $("#"+$(this).val()).css("background-color","#F4D0D7");//设置改变颜色
                            });
                        }else{    
                            $("input[name='check']").each(function(){
                                $(this).prop("checked",false);//设置不选中
                                for(var i = 0;i<colorList.length;i++){
                                    if($(this).prop("id")==colorList[i].id){
                                        $("#"+$(this).val()).css("background-color",colorList[i].color);//当不选中状态时，行的颜色变回原来的
                                        break;
                                    }
                                }
                                
                                
                            });
                        }    
               });  
                
                
            });
            
          //选中复选框，当前行改变颜色
            function changeColor(obj){
                if ($(obj).is(':checked')) {
                    $("#"+$(obj).val()).css("background-color","#F4D0D7");
                 }else{
                     for(var i = 0;i<colorList.length;i++){
                        if($(obj).prop("id")==colorList[i].id){
                            $("#"+$(obj).val()).css("background-color",colorList[i].color);//当不选中状态时，行的颜色变回原来的
                            break;
                        }
                    }
                 }
                
            }
            
            //停用;启用
            function changeStatus(status){
                var msg = "<spring:message code="message.button.stop"/>";
                var url = "${ctx}/dicPhar/enable.html";
                if(status==1){
                    msg = "<spring:message code="message.button.start"/>";
                    url = "${ctx}/dicPhar/disable.html";
                }
                var list = [];
                $("input[name='check']").each(function(){
                     if ($(this).is(':checked')) {
                        list.push($("#h"+$(this).val()).val());
                     } 
                });
                
                if (list.length <= 0) {
                    $.alert("<spring:message code="message.lable.pleaseSelect"/>"+msg+"<spring:message code="message.listForm.YaoLiFenLei"/>", "warn");
                    return;
                }
                //
                var list = [];
                var delList = [];
                var data = "";
                $("input[name='check']").each(function(){
                     if ($(this).is(':checked')) {
                         if($("#s"+$(this).val()).val()==status){
                             delList.push($(this).val());
                         }else{
                             list.push($("#h"+$(this).val()).val());
                             data += $("#n"+$(this).val()).val()+",";
                         }
                     } 
                });
                
             
                
                if (list.length <= 0) {
                    $.alert("<spring:message code="message.lable.pleaseSelect"/>"+msg+"<spring:message code="message.lable.OKData"/>", "warn");
                    if($("#checkAll").prop("checked")){
                        $("#checkAll").click();
                    }else{
                        for(var i = 0;i<delList.length;i++){
                            $("#w"+delList[i]).click();
                        }
                    }
                    
                    return;
                }
                data = data.substring(0,data.length-1);

                for(var i = 0;i<delList.length;i++){
                    $("#w"+delList[i]).click();
                }
                
                $.HN.message.confirm("<spring:message code="message.lable.OKYES"/>"+msg+"<spring:message code="message.lable.FlowerDataYESorNo"/><br/>"+data, '', '').on(function (e) {
                    if (e) {
                        $.post(url, {
                            "array" : json.jsonToString(list), "status":status
                        }, function(result) {
                            if (result.success) {
                                $.alert("<spring:message code="message.HN.alert.chengGongCaoZuo"/>"+list.length+"<spring:message code="message.HN.alert.tiaoShuJu"/>",  "success");
                                search();
                            } else {
                                $.alert(result.msg || "<spring:message code="message.HN.alert.fail"/>", "error");
                                search();
                            }
                        }, "json");
                    }
                });
            }
           function select(){
               $("#rootType").HNSelect({
                    url: "${ctx}/selectController/getPhar.html", 
                    data: { ID: '' },
                    defaultText: "<option value=''><spring:message code="message.select.option"/></option>",
                    defaultselect: "",
                    func: function () {
                        $("#dicPhar1").HNSelect({
                            parent_selector: "#rootType", 
                            url: "${ctx}/selectController/getPhar.html", 
                            dataid: "ID", 
                            defaultText: "<option value=''><spring:message code="message.select.option"/></option>",
                            defaultselect: "",
                            func: function(){
                                $("#dicPhar2").HNSelect({
                                    parent_selector: "#dicPhar1", 
                                    url: "${ctx}/selectController/getPhar.html", 
                                    dataid: "ID", 
                                    defaultText: "<option value=''><spring:message code="message.select.option"/></option>",
                                    defaultselect: ""
                                });
                            }
                        });
                    }
                });
           }
           function exportExcel(){
        		$.HN.message.confirm('<spring:message code="message.lable.OKExport"/>&nbsp;<spring:message code="message.lable.pharxls"/>&nbsp;<spring:message code="message.lable.ma"/>', '', '').on(function (e) {
   	               if (e) {
   	            	  $("#exportExcel").submit();
   			
   	               }
             	 }); 
             	
        	} 
    </script>
</body>
</html>
